<?php
/**

 */
define("CACHE_PREFIX_QQ_TOKEN","_qq_token_");
class db_qq_token extends db_base {
    
	var $table_name = "qq_token";
	
	function db_qq_token()
	{
		parent::db_base();
	}
	
	function get_item_by_id($id)
	{
		
		$key = CACHE_PREFIX_QQ_TOKEN . "-get_item_by_id-{$id}";
		$mc = mem_cache::get_instance();
		$cache = $mc->get($key);
		if ($cache === false)
		{
			$where = " id = '{$id}' ";
			$content = $this->get_alllist($this->table_name,1,1, "", $where);
			$mc->set($key, serialize($content), 0, CACHE_EXPIRE_MIN_10);
		}
		else
		{
			$content = unserialize($cache);
		}
		
		return $content[0];
		
	}
	//获取默认用来抓取的token
	function get_item_is_used()
	{
		$key = CACHE_PREFIX_QQ_TOKEN . "-get_item_is_used";
		$mc = mem_cache::get_instance();
		$cache = $mc->get($key);
		if ($cache === false)
		{
			$now = date("Y-m-d H:i:s");
			$where = " is_used = 1 and end_time > '{$now}' ";
			$content = $this->get_alllist($this->table_name,1,1, "", $where);
			
			if(!$content)
			{
				$next_one = $this->change_item_used();
				if($next_one)
				{
					$content[0] = $next_one;
				}
			}
			
			$mc->set($key, serialize($content), 0, CACHE_EXPIRE_MIN_10);
		}
		else
		{
			$content = unserialize($cache);
		}
		
		return $content[0];
		
	}
	//更新默认用来抓取的token
	function change_item_used()
	{
		$get_code = $this->get_last_used_id();
		$old_id = intval($get_code["id"]);
		$next_one = $this->get_next_used_id($old_id);
		
		if(!$next_one)
		{
			$next_one = $this->get_next_used_id(0);
		}
	
		if($next_one)
		{
			$sql = "update  {$this->table_name}  set is_used =0 ";
			$this->query($sql);
			
			$sql = "update  {$this->table_name}  set is_used =1 where id =  {$next_one["id"]}";
			$this->query($sql);
			
			
			$key = CACHE_PREFIX_QQ_TOKEN . "-get_item_is_used";
			$mc = mem_cache::get_instance();
			$cache = $mc->delete($key);
			
		}
		else
		{
			return false;
		}
		
		
		
		
		return $next_one;
		
	}
	
	function get_last_used_id()
	{
		$where = " is_used = 1  ";
		$content = $this->get_alllist($this->table_name,1,1, "", $where);
		
		return $content[0] ;
	}
	
	function get_next_used_id($id)
	{
		$now = date("Y-m-d H:i:s");
		$sql = "select min(id) as id  from {$this->table_name} where id > $id and end_time > '{$now}'";
		$content = $this->select($sql);
		$id= $content[0]["id"];
		
		
		$content = $this->get_item_by_id($id);
		
		return $content;
	}
	
	/**
	 * 用户注册
	 * @param $user_name
	 * @param $password
	 */
	function insert_qq_token($t_access_token,$t_refresh_token,$t_expire_in,$t_code,$t_openid,$t_openkey)
	{
		// 插入数据
		
		$ary["t_access_token"] = $t_access_token;
		$ary["t_refresh_token"] = $t_refresh_token;
		$ary["t_expire_in"] = $t_expire_in;
		$ary["t_code"] = $t_code;
		$ary["t_openid"] = $t_openid;
		$ary["t_openkey"] = $t_openkey;
		$ary["create_time"] = Date::get_date_time();
		$ary["end_time"] = date("Y-m-d H:i:s",time()+$t_expire_in);
		
		$ret = $this->insert($this->table_name, $ary,"",true);
		
		
		return $ret;
	}
	
	
	
}



